/**
 * add Queue support
 */
import Queue from '@/utils/queue';

export default {
  data() {
    return {
      tasks: {
        timer: null,
        queue: Queue.newQueue(),
      }
    }
  },
  methods: {
    __task_add: function (task) {
      this.tasks.queue.push(task);
      if (this.tasks.timer == null) {
        this.__task_run();
      }
    },
    __task_run: function () {
      this.tasks.timer = setTimeout(() => {
        this.__task_run_();
      }, 0)
    },
    __task_run_: function () {
      var task = this.tasks.queue.pop();
      if (task != null && typeof (task) === 'function') {
        task().then(() => {
          this.__task_run();
        }, () => {
          this.__task_run();
        }).catch(() => {
          this.__task_run();
        });
      } else {
        this.tasks.timer = null;
      }
    },
  }
}
